package com.booklove.service.book;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.booklove.domain.dto.PageDTO;
import com.booklove.domain.po.Wishbook;
import com.booklove.domain.query.WishbookQuery;
import com.booklove.domain.vo.WishbookVO;
import com.booklove.mapper.WishbookMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 许愿图书 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-09-21
 */
@Service
public class WishbookServiceImpl extends ServiceImpl<WishbookMapper, Wishbook> implements WishbookService {

    @Override
    public PageDTO<WishbookVO> queryWishbooksPage(WishbookQuery wishbookQuery) {
        // 1.添加排序规则
        // 使用内置的函数转换并且 设置规则
        List<OrderItem> orderItems = wishbookQuery.toMpOrderItems();

        // 获取分页对象和排序情况
        Page<WishbookVO> page = wishbookQuery.toMpPageDefaultSortByIdAsc(orderItems.toArray(new OrderItem[0]));
        /*
        查询
         */
        Page<WishbookVO> p = baseMapper.selectPageVO(page, wishbookQuery.getName());

        // 获取数据信息
        return PageDTO.of(p, WishbookVO.class);
    }

    @Override
    public WishbookVO getVOById(Integer id) {
        WishbookVO wishbookVO = baseMapper.getVOById(id);
        return wishbookVO;
    }
}
